package com.lanyun.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lanyun.pojo.*;
import com.lanyun.service.CustomerService;
import com.lanyun.service.ProductService;
import com.lanyun.service.ProductTypeService;
import com.lanyun.service.SalesOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/salesOrder")
public class SalesOrderController {
    @Autowired
    private SalesOrderService salesOrderService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductTypeService productTypeService;

    @RequestMapping("/getAll")
    public String getAll(Model model, HttpSession session){
        Admin loginer = (Admin) session.getAttribute("admin");
        String roleName = loginer.getRole().getName();
        List<SalesOrder> salesOrders = salesOrderService.getAll();
        model.addAttribute("salesorders",salesOrders);
        model.addAttribute("roleName",roleName);
        return "salesorder_list";
    }

    @RequestMapping("/toAdd")
    public String getAllCustomers(Model model){
        List<Customer> customers = customerService.getAll();
        List<ProductType> protypes = productTypeService.getAll();
        model.addAttribute("customers",customers);
        model.addAttribute("protypes",protypes);
        return "salesorder_add";
    }

    @RequestMapping("/getProductByType")
    @ResponseBody
    public Object getProductByType(String typeid){
        return productService.getProductByType(typeid);
    }

    @RequestMapping("/add")
    @ResponseBody
    public Object add(HttpSession session,String cid,@RequestParam("pids[]") String[] pids,@RequestParam("amounts[]") String[] amounts){
        Admin salesman = (Admin) session.getAttribute("admin");
        String salesmanId = String.valueOf(salesman.getId());
        int result = salesOrderService.addOrderAndDetail(cid, salesmanId, pids, amounts);
        return result;
    }
    
    @RequestMapping("/getDetail")
    @ResponseBody
    public Object getDetail(String oid){
        SalesOrder salesOrder = salesOrderService.getById(oid);
        List<Map<String,String>> details = salesOrderService.getDetailsById(oid);
        Map data = new HashMap();
        data.put("salesOrder",salesOrder);
        data.put("details",details);
        return data;
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(Model model,String oid){
        SalesOrder salesOrder = salesOrderService.getById(oid);
        List<Map<String,String>> details = salesOrderService.getDetailsById(oid);
        List<Customer> customers = customerService.getAll();
        List<ProductType> protypes = productTypeService.getAll();
        model.addAttribute("salesOrder",salesOrder);
        model.addAttribute("details",details);
        model.addAttribute("customers",customers);
        model.addAttribute("protypes",protypes);
        return "salesorder_update";
    }

    @RequestMapping("/updateById")
    @ResponseBody
    public Object updateById(String oid,@RequestParam("pids[]") String[] pids,@RequestParam("amounts[]") String[] amounts){
        System.out.println("oid:"+oid);
        return salesOrderService.updateDetail(oid,pids,amounts);
    }

    @RequestMapping("/getOutProducts")
    @ResponseBody
    public Object getOutProducts(String oid,HttpSession session){
        Admin keeper = (Admin) session.getAttribute("admin");
        String keeperId = String.valueOf(keeper.getId());
        return salesOrderService.getProductsByKeeperAndOrder(oid,keeperId);
    }

    @RequestMapping("/outWarehouse")
    @ResponseBody
    public Object outWarehouse(String oid,String wid,String pid,String amount,HttpSession session){
        Admin keeper = (Admin) session.getAttribute("admin");
        String keeperId = String.valueOf(keeper.getId());
        int result = salesOrderService.outWareHouse(oid,pid,wid,amount,keeperId);
        return result;
    }

    @RequestMapping("/getByCondition")
    public String getByCondition(Model model,String customerName, String salesmanName, String keeperName,
                                 @DateTimeFormat(pattern="yyyy-MM-dd")Date enterDateStart,
                                 @DateTimeFormat(pattern="yyyy-MM-dd")Date enterDateEnd,
                                 @DateTimeFormat(pattern="yyyy-MM-dd")Date outDateStart,
                                 @DateTimeFormat(pattern="yyyy-MM-dd")Date outDateEnd,
                                 String state){
        List<SalesOrder> salesorders = salesOrderService.getByCondition(customerName, salesmanName, keeperName, enterDateStart, enterDateEnd, outDateStart, outDateEnd, state);
        model.addAttribute("salesorders",salesorders);
        return "salesorder_list::orderlist";
    }

    @RequestMapping("/delById")
    @ResponseBody
    public Object delById(String oid){
        return salesOrderService.delById(oid);
    }

    @RequestMapping("/delBatch")
    @ResponseBody
    public Object delBatch(@RequestParam("ids[]") String[] ids){
        return salesOrderService.delBatch(ids);
    }

    @RequestMapping("/isBegined")
    @ResponseBody
    public Object isBegined(@RequestParam String oid){
        return salesOrderService.isBegined(oid);
    }

    @RequestMapping("/getByKeeper")
    public Object getByKeeper(HttpSession session,Model model) {
        Admin keeper = (Admin) session.getAttribute("admin");
        List<SalesOrder> salesorders = salesOrderService.getByKeeper(keeper);
        model.addAttribute("salesorders",salesorders);
        return "salesorder_list::orderlist";
    }
}
